<template>
    <d2-container>

        <h3>请选择需要购买的教材:</h3>

        <el-checkbox-group v-model="wait_to_buy_product">
            <el-checkbox-button v-for="product in product_list" :label="product._id" :key="product._id">
                {{product.name}}
            </el-checkbox-button>
        </el-checkbox-group>


        <h3>选择需要购买的学生</h3>

        <template>
            <el-transfer v-model="student_chosen" :data="wait_buy_student" >

            </el-transfer>
        </template>


        <h3 style="display: inline-block;">开始生成订单
            <el-button @click="generateProductOrder" type="primary" style="margin-left: 40px">
                确认订购
            </el-button>
        </h3>

    </d2-container>
</template>

<script>
    import request from "@/plugin/axios";
    import {mapState} from "vuex";

    export default {
        data() {
            return {
                class_id: undefined,
                wait_buy_student: [],
                student_chosen:[],
                wait_to_buy_product: [],
                product_list: []
            }
        },
          computed: {
            ...mapState('d2admin/user', ['info']),
        },
        methods: {

            getProcurableProduct() {
                request({
                    url: "/product/getProductList",
                    method: "post",
                    data: {page: 1, pageSize: 99999,school:this.school}
                }).then(res => {
                    if (res.code == 200) {

                        this.product_list = res.data.product
                        console.log(this.product_list)
                    }
                });
            },

            getClassStudent() {
                request({
                    url: "/class/getClassStudentList",
                    method: "post",
                    data: {_id: this.class_id}
                }).then(res => {
                    if (res.code == 200) {

                        this.class_student = res.data


                        res.data.forEach((value, index) => {
                            this.wait_buy_student.push({key: value.student._id, label: value.student.name})
                        })
                    }
                });
            },
            generateProductOrder() {
                request({
                    url: "/productOrder/addProductOrderBatch",
                    method: "post",
                    data: {
                        student_chosen: this.student_chosen,
                        wait_to_buy_product: this.wait_to_buy_product,
                        agent : this.agent,
                        school : this.school
                    }
                }).then(res => {
                    if (res.code == 200) {
                        this.$message({
                            type: "info",
                            message: "订购教材成功"
                        });
                        // this.$router.push({path: "/product-order-list"});
                       this.refresh  = true
                        this.$router.back(-1)
                                
                    }
                });
            }
        },
        beforeRouteLeave(to,from,next){
    if (this.refresh){
      to.meta.refresh = 'a'
      this.refresh = undefined
    }
    next()
  },
  
        created() {
            this.school = this.info.school[0]._id
            this.agent = this.info.staff == undefined ? null : this.info.staff._id
            this.staffname = this.info.staff == undefined ? '' : this.info.staff.name
            if (this.$route.query.id != undefined) {

                this.class_id = this.$route.query.id

                this.getClassStudent()

                this.getProcurableProduct()
            }
        }
    };
</script>
